/************************* 版本声明 *********************************
 *@since jdk1.5
 *@version 0.1
 ************************* 变更记录 *********************************
 *
 * 创建者：bijiqiang Email:bijiqiang@gmail.com   创建日期： 2010-1-5
 * 创建记录：创建类结构。
 * ……
 ************************* To  Do *********************************
 */

package com.realgodo.site.service.site;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.hibernate.DefaultEntityManager;

import com.realgodo.site.entity.site.Event;

@Service("eventManager")
@Transactional
public class EventManager extends DefaultEntityManager<Event, Long> {

	@Autowired
	protected SessionFactory sessionFactory;

	public Page<Event> findEvents(int pageSize, int overFlag) {
		Page<Event> page = new Page<Event>(pageSize);
		Criteria c = sessionFactory.getCurrentSession().createCriteria(
				Event.class);
		if (overFlag != Event.OVER_ALL) {
			c.add(Restrictions.eq("over", overFlag));
		}
		c.addOrder(Order.desc("createDate"));
		return this.entityDao.findByCriteria(page, c);
	}

	public Event createEvent(Event event) {
		return (Event) this.saveMerge(event);
	}

	public void removeEvent(Long[] ids) {
		for (Long id : ids) {
			this.delete(id);
		}
	}

	public Event topEvent(Long id) {
		Event event = this.get(id);
		event.setTop(Event.TOP_YES);
		return (Event) this.saveMerge(event);
	}

	public Event overEvent(Long id) {
		Event event = this.get(id);
		event.setOver(Event.OVER_YES);
		return (Event) this.saveMerge(event);
	}

	public List<Event> findFontEvents(int maxResult) {
		Page<Event> page = new Page<Event>(maxResult);
		Criteria c = sessionFactory.getCurrentSession().createCriteria(
				Event.class);
		c.add(Restrictions.eq("over", Event.OVER_NOT));
		c.addOrder(Order.desc("top"));
		c.addOrder(Order.desc("createDate"));
		return this.entityDao.findByCriteria(page, c).getResult();
	}
}
